﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Public {
    public partial class Frm查找 :Form {
        private DataGridView dgv;
        private int ind=0;
        private int maxInd=0;
        public Frm查找(DataGridView dgv) {
            InitializeComponent();
            this.dgv = dgv;
            maxInd = dgv.Rows.Count - 1;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnFind_Click(object sender, EventArgs e) {
            //this.DialogResult = DialogResult.OK;
            if (dgv==null || dgv.Rows.Count<=0) {
                return;
            }

            if (rBtnUp.Checked) {
                for (int i = maxInd; i >= 0; i--) {
                    maxInd--;
                    DataGridViewRow row = dgv.Rows[i];
                    bool flag = false;
                    foreach (DataGridViewCell cell in row.Cells) {
                        if (cell.Value != null) {
                            if (cell.Value.ToString().Contains(cmValue.Text)) {
                                row.Selected = true;
                                dgv.FirstDisplayedScrollingRowIndex = i;
                                dgv.CurrentCell = row.Cells[0];
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (flag) {
                        break;
                    }
                }
                if (maxInd<=0) {
                    DialogResult r=   MessageBox.Show("已到达第一行,是否重新查询?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (r==DialogResult.Yes) {
                        maxInd = dgv.Rows.Count - 1;
                    }
                }
            } else if (rBtnDown.Checked) {
                for (int i = ind; i < dgv.Rows.Count - 1; i++) {
                    ind++;
                    DataGridViewRow row = dgv.Rows[i];
                    bool flag = false;
                    foreach (DataGridViewCell cell in row.Cells) {
                        if (cell.Value != null) {
                            if (cell.Value.ToString().Contains(cmValue.Text)) {
                                row.Selected = true;
                                dgv.FirstDisplayedScrollingRowIndex = i;
                                dgv.CurrentCell = row.Cells[0];
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (flag) {
                        break;
                    }
                }
                if (ind >= dgv.Rows.Count - 1) {
                    DialogResult r = MessageBox.Show("已到达最后一行,是否重新查询?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (r == DialogResult.Yes) {
                        ind = 0;
                    }
                }
            }

            

        }

        private void cmValue_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Enter) {
                this.btnFind_Click(sender, e);
                btnFind.Focus();
            }
        }
    }
}
